package com.becca.cake.controller.statistic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.order.OrderModel;
import com.becca.cake.model.statistics.OrderTypeCountModel;
import com.becca.cake.model.statistics.SaleReportModel;
import com.becca.cake.model.statistics.UserConsumptionModel;
import com.becca.cake.service.statistcs.IStatisticsService;
import com.becca.cake.util.JsonUtil;

@Controller
@RequestMapping("/statisticManager")
public class StatisticAction extends BaseAction {
    private final static Log   logger = LogFactory.getLog(StatisticAction.class);
    @Autowired
    private IStatisticsService iStatisticsService;

    /**
        * 商品推荐列表查询
        * @param advertModel
        * @return
        */
    @RequestMapping(value = "/selectSatasticOrderGoods", method = RequestMethod.POST)
    @ResponseBody
    public String selectSatasticOrderGoods(String stasticGoodsStartDate, String stasticGoodsEndDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("stasticGoodsStartDate", stasticGoodsStartDate);
        map.put("stasticGoodsEndDate", stasticGoodsEndDate);
        try {
            // stmp1:获取订单数据
            List<OrderModel> list = this.iStatisticsService.selectSatasticOrderGoods(map);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 订单统计导出Excel
     * @param deliveryDate
     */
    @RequestMapping(value = "/exportStatisticsOrder", method = RequestMethod.GET)
    public void exportStatisticsOrder(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value = "deliveryDate", required = false) String deliveryDate) {
        try {
            //step1:导出订单统计数据
            this.iStatisticsService.exportStatisticsOrder(request, response, deliveryDate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("订单统计导出Excel失败!", e);
            }
        }
    }

    /**
     * 获取订单优惠码数据
     * @param statisticCouponCodeStartDate
     * @param statisticCouponCodeEndDate
     * @return
     */
    @RequestMapping(value = "/selectSatasticCouponCode", method = RequestMethod.POST)
    @ResponseBody
    public String selectSatasticCouponCode(String couponCode, String statisticCouponCodeStartDate,
                                           String statisticCouponCodeEndDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderStatus", "5");
        map.put("couponType", "2");
        map.put("couponCode", couponCode);
        map.put("statisticCouponCodeStartDate", statisticCouponCodeStartDate);
        map.put("statisticCouponCodeEndDate", statisticCouponCodeEndDate);
        try {
            // stmp1:获取订单优惠码数据
            List<OrderModel> list = this.iStatisticsService.selectSatasticCouponCode(map);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 优惠码统计导出Excel
     * @param deliveryDate
     */
    @RequestMapping(value = "/exportStatisticsCouponCode", method = RequestMethod.GET)
    public void exportStatisticsCouponCode(HttpServletRequest request,
                                           HttpServletResponse response,
                                           @RequestParam(value = "couponCode", required = false) String couponCode,
                                           @RequestParam(value = "statisticCouponCodeStartDate", required = false) String statisticCouponCodeStartDate,
                                           @RequestParam(value = "statisticCouponCodeEndDate", required = false) String statisticCouponCodeEndDate) {
        try {
            //step1:导出订单统计数据
            this.iStatisticsService.exportStatisticsCouponCode(request, response, couponCode,
                statisticCouponCodeStartDate, statisticCouponCodeEndDate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("优惠码统计导出Excel失败!", e);
            }
        }
    }

    /**
     * 订单类型统计图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectCountOrderByOrderType", method = RequestMethod.POST)
    @ResponseBody
    public String selectCountOrderByOrderType(@RequestParam(value = "radioDay", required = false) Integer radioDay,
                                              @RequestParam(value = "orderPieStartDate", required = false) String orderPieStartDate,
                                              @RequestParam(value = "orderPieEndDate", required = false) String orderPieEndDate) {
        try {
            //step1:查询订单类型信息
            List<OrderTypeCountModel> list = this.iStatisticsService.selectCountOrderByOrderType(
                radioDay, orderPieStartDate, orderPieEndDate);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 订单销售金额统计图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectCountOrderAmountByOrderType", method = RequestMethod.POST)
    @ResponseBody
    public String selectCountOrderAmountByOrderType(@RequestParam(value = "radioDay", required = false) Integer radioDay,
                                                    @RequestParam(value = "orderPieStartDate", required = false) String orderPieStartDate,
                                                    @RequestParam(value = "orderPieEndDate", required = false) String orderPieEndDate) {
        try {
            //step1:查询订单销售金额信息
            Map<String, Object> mapCountOrder = this.iStatisticsService
                .selectCountOrderAmountByOrderType(radioDay, orderPieStartDate, orderPieEndDate);
            return JsonUtil.writeObjectNoPageJSON(mapCountOrder);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * sku统计图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectCountOrderBySku", method = RequestMethod.POST)
    @ResponseBody
    public String selectCountOrderBySku(@RequestParam(value = "radioDay", required = false) Integer radioDay,
                                        @RequestParam(value = "orderPieStartDate", required = false) String orderPieStartDate,
                                        @RequestParam(value = "orderPieEndDate", required = false) String orderPieEndDate) {
        try {
            //step1:查询订单类型信息
            List<OrderTypeCountModel> list = this.iStatisticsService.selectCountOrderBySku(
                radioDay, orderPieStartDate, orderPieEndDate);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     *查询订单统计报表数据
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/selectOrderReportInfo", method = RequestMethod.POST)
    @ResponseBody
    public String selectOrderReportInfo(@RequestParam(value = "radioDay", required = false) Integer radioDay,
                                        @RequestParam(value = "orderPieStartDate", required = false) String orderPieStartDate,
                                        @RequestParam(value = "orderPieEndDate", required = false) String orderPieEndDate) {
        try {
            OrderModel orderModel = this.iStatisticsService.selectOrderReportInfo(radioDay,
                orderPieStartDate, orderPieEndDate);
            return JsonUtil.writeObjectFormJSON(orderModel);
        } catch (Exception e) {
            logger.error("获取订单统计报表数据失败!");
            return JsonUtil.returnJsonUnSuccess("获取订单统计报表数据失败!");
        }
    }

    /**
     * 订单增长统计图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectOrderRoseChart", method = RequestMethod.POST)
    @ResponseBody
    public String selectOrderRoseChart(@RequestParam(value = "radioBoxDay", required = false) Integer radioBoxDay,
                                       @RequestParam(value = "orderRoseStartDate", required = false) String orderRoseStartDate,
                                       @RequestParam(value = "orderRoseEndDate", required = false) String orderRoseEndDate) {
        try {
            //step1:订单增长统计图表
            Map<String, Object> mapCountOrder = this.iStatisticsService.selectOrderRoseChart(
                radioBoxDay, orderRoseStartDate, orderRoseEndDate);
            return JsonUtil.writeObjectNoPageJSON(mapCountOrder);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询全部有效订单地址订单
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectAllOrderAddressPoint", method = RequestMethod.POST)
    @ResponseBody
    public String selectAllOrderAddressPoint(HttpServletRequest request,
                                             HttpServletResponse response,
                                             @RequestParam(value = "orderTypes", required = false) String orderTypes) {
        try {
            //step1:查询全部有效订单地址订单
            List<OrderModel> list = this.iStatisticsService.selectAllOrderAddressPoint(orderTypes);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 销售列表查询
     * @param 
     * @return
     */
 @RequestMapping(value = "/selectSatasticOrderSale", method = RequestMethod.POST)
 @ResponseBody
 public String selectSatasticOrderSale(String year, String month) {
     Map<String, Object> map = new HashMap<String, Object>();
     
     	 String createDate = year+month; 
    	 map.put("year", year);
         map.put("month", month);
         map.put("createDate", createDate);
 
     try {
         List<SaleReportModel> list = this.iStatisticsService.selectSatasticOrderSale(map);
         return JsonUtil.writeObjectNoPageJSON(list);
     } catch (Exception e) {
         return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
     }
 }
 
	 /**
	  * 用户消费查询
	  * @param userConsumptionModel
	  * @return
	  */
	@RequestMapping(value = "/selectSatasticUserConsumption", method = RequestMethod.POST)
	@ResponseBody
	public String selectSatasticUserConsumption(@ModelAttribute UserConsumptionModel userConsumptionModel) {
		//step1:获取请求参数
        PagingBean page = getInitPagingSortBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userConsumptionModel", userConsumptionModel);
        map.put("pagingBean", page);
	  try {
	      // stmp1:获取订单数据
	      List<UserConsumptionModel> list = this.iStatisticsService.selectUserConsumption(map);
	      long count = this.iStatisticsService.selectUserConsumptionCount(map);
	      return JsonUtil.writeObjectPageJSON(list, count);
	  } catch (Exception e) {
	      return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
	  }
	}
	
	 /**
	  * 用户消费查询
	  * @param userConsumptionModel
	  * @return
	  */
	@RequestMapping(value = "/selectSatasticUserConsumptionDetail", method = RequestMethod.POST)
	@ResponseBody
	public String selectSatasticUserConsumptionDetail(@RequestParam(value = "userId") Integer userId) {
		//step1:获取请求参数
       PagingBean page = getInitPagingSortBean();
       Map<String, Object> map = new HashMap<String, Object>();
       map.put("userId", userId);
       map.put("pagingBean", page);
	  try {
	      // stmp2:获取订单数据
	      List<UserConsumptionModel> list = this.iStatisticsService.selectUserConsumptionDetail(map);
	      long count = this.iStatisticsService.selectUserConsumptionDetailCount(map);
	      return JsonUtil.writeObjectPageJSON(list, count);
	  } catch (Exception e) {
	      return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
	  }
	}
	
    /**
     * 渠道关注统计图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectFollowSourceChart", method = RequestMethod.POST)
    @ResponseBody
    public String selectFollowSourceChart(@RequestParam(value = "radioBoxDay", required = false) Integer radioBoxDay,
                                       @RequestParam(value = "followSourceStartDate", required = false) String followSourceStartDate,
                                       @RequestParam(value = "followSourceEndDate", required = false) String followSourceEndDate) {
        try {
            //step1:渠道关注统计图表
            Map<String, Object> mapCountOrder = this.iStatisticsService.selectFollowSourceChart(
                radioBoxDay, followSourceStartDate, followSourceEndDate);
            return JsonUtil.writeObjectNoPageJSON(mapCountOrder);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

}
